package com.bk.controller;

import java.sql.SQLException;
import java.util.ArrayList;

import com.cjw.model.DBMap;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.moomi.common.model.BkBooking;
import com.moomi.common.model.BkOrder;
import com.moomi.common.model.BkSelection;
import com.utils.Utils;

import booking.utils.BookedUtils;

public class BKBookingController extends BKAuthController {

	@Override
	public void index() {
		super.index();

		Integer page = getPage();
		String date = getPara("date");
		if (date == null) {
			date = Utils.dateNow("yyyy-MM-dd");
		}
		setTitle(date + "日预约管理");
		BookedUtils bookedUtils = new BookedUtils(getShopId());
		ArrayList<DBMap> items = bookedUtils.getBookedItems(page, date);
		// List<BkBooking> items = BkBooking.dao.search("shopId", getShopId());
		// List<BkBooking> items = BkBooking.dao.findAll();
		// setAttr("items", items);
		setItems(items);
		renderJson(items);
		render("table.html");
	}

	public void bookingUpdate() {
		super.update();
		BkBooking model = (BkBooking) updateModel(BkBooking.class);
	}

	public void bookingInfo() {
		Integer id = getParaToInt("id");
		BkBooking booking = BkBooking.dao.findById(id);
		Integer orderId = booking.getOrderId();
		BkOrder order = BkOrder.dao.findById(orderId);
		setAttr("booking", booking);
		setAttr("order", order);
		render("form.html");
	}

	public void selection() {
		int id = getId();
		BkSelection selection = BkSelection.dao.findById(id);
		setInfoItem(selection);
		String date = getPara("date");
		if (date == null) {
			date = Utils.dateNow("yyyy-MM-dd");
		}
		setTitle(date + "日预约管理");
		BookedUtils bookedUtils = new BookedUtils(getShopId());
		ArrayList<DBMap> items = bookedUtils.getBookedItems(1, date, id);
		setItems(items);
		render("tableSelection.html");
	}

	public void t() {
		BookedUtils bookedUtils = new BookedUtils(getShopId());
		renderJson(bookedUtils.getBookedItems(1, null));
		renderJson(BkBooking.dao.getBookingInfo(null, 12, null, null));
	}

	public void booking() {
		render("index.html");
	}

	public void arrive() {
		Integer id = getParaToInt("id");
		BkBooking booking = BkBooking.dao.findById(id);
		booking.setIsArrive(1);
		boolean flag = booking.update();
		renderFlag(flag);
	}

	@Override
	public void delete() {
		super.delete();
		Integer id = getParaToInt("id");
		BkBooking booking = BkBooking.dao.findById(id);
		Integer shouldPay = booking.getShouldPay();
		boolean flag = Db.tx(new IAtom() {

			@Override
			public boolean run() throws SQLException {
				if (shouldPay != null && shouldPay == 1) {
					BkOrder order = BkOrder.dao.findById(booking.getOrderId());
					if (order.getStatus().equals(1)) {
						boolean refundFlag = order.refund(order.getTotalFee());
						if (!refundFlag) {
							renderMsg("退款失败");
							return false;
						}
					}
				}
				booking.setStatus(-1);
				return booking.update();
			}
		});
		if (flag) {
			renderSuccess();
		} else {
			renderMsg("取消失败");
		}
	}
}
